package com.mark.utils;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.springframework.util.ClassUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class FileUtil {
    /**
     * 文件下载功能
     *
     * @param response 响应
     * @param fileName 下载的文件名
     */
    public static void downLoadFile(HttpServletResponse response, String fileName, String realPath) throws IOException {
        FileInputStream fileInputStream = null;
        ServletOutputStream outputStream = null;
        String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        File file = new File(filePath + realPath);
        try {
            //将文件写入流中
            fileInputStream = new FileInputStream(file);
            //1、清空响应区
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setContentType("application/x-download");
            //3、下载的文件名  attachment 附带文件下载  online 在线打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            //response.addHeader("Content-Length",""+file.length());
            //文件流循环写入到响应流中
            outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }


    /**
     * 读取Excel表格的内容
     *
     * @param file 要读取的文件
     * @return 读取的内容  ArrayList<ArrayList<String>>
     */
    public static ArrayList<ArrayList<String>> readExcel(File file) {
        ArrayList<ArrayList<String>> aa = new ArrayList<>();
        Workbook workbook = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            workbook = Workbook.getWorkbook(fileInputStream);
            Sheet sheetA = workbook.getSheet(0);
            for (int i = 0; i < sheetA.getRows(); i++) {
                ArrayList<String> a = new ArrayList<>();
                for (int j = 0; j < sheetA.getColumns(); j++) {
                    Cell cell = sheetA.getCell(j, i);
                    a.add(cell.getContents());
                }
                aa.add(a);
            }
            //读取文件后删除文件
            file.delete();
            return aa;
        } catch (Exception e) {
            return aa;
        } finally {
            if (workbook != null) {
                workbook.close();
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
